home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / lzw.arc / LZWCOM.C < prev    next >
Text File  |  1985-08-20  |  3KB  |  87 lines

  1. /* LZWCOM - FILE COMPRESSOR UTILITY                                     */
  2. #include "stdio.h"
  3. #include "debug.h"
  4. #define FALSE    0
  5. #define TRUE     !FALSE
  6. #define TABSIZE  4096
  7. #define NO_PRED  0xFFFF
  8. #define EMPTY   0xFFFF 
  9. #define NOT_FND 0xFFFF 
  10. #define UEOF ((unsigned)EOF)
  11. struct entry {
  12.   char used;
  13.   unsigned int next;      /* hi bit is 'used' flag                        */
  14.   unsigned int predecessor;               /* 12 bit code                  */
  15.   unsigned char follower;
  16. } string_tab[TABSIZE];
  17.  
  18. /*   routines common to compress and decompress, contained in CommLZW.c */
  19. unsigned hash();
  20. unsigned unhash();
  21. unsigned getcode();
  22. putcode();
  23. init_tab();
  24. upd_tab();
  25.  
  26. FILE *op;
  27. main(argc,argv)
  28. int argc; char *argv[];
  29. {
  30.   register unsigned int c, code, localcode;
  31.   int code_count = TABSIZE - 256;
  32.   FILE *infd, *outfd;
  33.   if (3 != argc) {
  34.     fprintf(stderr,"Usage : lzwcom oldfilename squeezefilename\n");
  35.     exit(0);
  36.   }
  37.   if ( -1 == (infd = fopen( *++argv, "r")) ) {
  38.     fprintf(stderr,"Cant open %s\n", *argv);
  39.     exit(0);
  40.   }
  41.   if ( -1 == (outfd = fopen(*++argv,"w")) ) {
  42.     fprintf(stderr,"Cant create %s\n",*argv);
  43.  
  44.     exit(0);
  45.   }
  46.   init_tab();                           /* initialize code table        */
  47.   c = getc(infd);
  48.   code = unhash(NO_PRED,c);             /* initial code for table       */
  49.   DEBUGGER (\
  50.     if (c >= ' ' || c <= '~' || c == '\n' || c == '\r')\
  51.           putchar(c);\
  52.     else\
  53.         printf("[%2x]",c);\
  54.   )
  55.   while ( UEOF != (c = getc(infd)) ) 
  56.   {
  57.       DEBUGGER (\
  58.         if (c >= ' ' || c <= '~' || c == '\n' || c == '\r')\
  59.               putchar(c);\
  60.         else\
  61.             printf("[%2x]",c);\
  62.       )
  63.       if ( NOT_FND != (localcode = unhash(code,c)) ) 
  64.       {
  65.           code = localcode;
  66.           continue;
  67.       }
  68. /* when the above clause comes false, you have found the last known code */
  69.       putcode(outfd,code);      /* only update table if table isn't full */
  70.       DEBUGGER(printf( "\n%x\n",code);)
  71.       if ( code_count ) 
  72.       {
  73.           upd_tab(code,c);
  74.           DEBUGGER(printf("\nadding %x %c = %x\n",code,c,unhash(code,c));)
  75.       --code_count;
  76.       }
  77. /* start loop again with the char that didn't fit into last string      */
  78.       code = unhash(NO_PRED,c);
  79.   }
  80.   putcode(outfd,code);                  /* once EOF reached, always     */
  81.                                         /* one code left unsent         */
  82.   DEBUGGER(fprintf(stderr,"\n%x\n",code);)
  83.   flushout(outfd);                      /* make sure everything's written */
  84.   exit(0);                              /* make sure everything gets closed */
  85. }
  86.  
  87.